<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-8
 * Time: 下午2:54
 */

namespace App\Http\Controllers\Home;


use App\Helpers;
use App\Http\Controllers\Controller;
use App\Repositories;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Cache;

abstract class BaseController extends Controller
{
    use Helpers\Jump;

    public function __construct()
    {
        $this->setTemplatePrefix('home.')->setCurrentModule('home');

        $list = (new Repositories\Admin\ConfigRepository())->getConfigList();

        config($list);

        //  友情链接
//        $this->withData('linksList', Repositories\Basic\LinkRepository::getInstance()->getLinkList());

        //  banner图列表
//        $this->withData('advHomeTopBanner', Repositories\Basic\AdvRepository::getInstance()->getAdvDataListByName('home_top_banner_list'));

        //  企业公示栏目列表
//        $this->withData('gongShiNavList', Repositories\Portal\CategoryRepository::getInstance()->getSelectModel(['pid' => 7])->get());

        //  首页导航

        $this->withData('todayWeather', $this->getWeatherByArea());
    }

    /**
     * 景区天气
     *
     * @return \Illuminate\Http\Response
     */
    protected function getWeatherByArea()
    {
        $weather = Cache::get('today-weather');
        if (!$weather) {
            $url = 'https://ali-weather.showapi.com/area-to-weather';
            $appcode = '6cc8525cdf2d4638a1aca5f362a17316';
            $client = new Client();
            $resp = $client->request('GET', $url, ['query' => ['area' => '统景温泉', 'need3HourForcast' => '0', 'needAlarm' => '0', 'needHourData' => '0', 'needIndex' => '0', 'needMoreDay' => '1'],
                'headers' => [
                    'Authorization' => 'APPCODE ' . $appcode,
                ]]);
            $weather = $resp->getBody();
            $weather = json_decode($weather->getContents(), true);
            $showapi_res_body = get_value($weather, 'showapi_res_body', []);
            $weather = get_value($showapi_res_body, 'now', []);
            Cache::set('today-weather', $weather, 60);
        }
        return $weather;
    }
    /**
     * 服务调用
     * @return mixed
     */
    abstract public function getServiceRepository();

    /**
     * 获取seo信息
     * @param $categoryDetail
     * @param $pCategoryDetail
     * @param $detail
     * @return $this
     */
    protected function getSeoInfo($categoryDetail, $pCategoryDetail, $detail)
    {
        $seo_title = config('seo_title', '');
        $seo_keywords = config('seo_keywords', '');
        $seo_description = config('seo_description', '');
        if (get_value($categoryDetail, 'seo_title', '')) {
            $seo_title = $categoryDetail->seo_title;
        } else if (get_value($pCategoryDetail, 'seo_title', '')) {
            $seo_title = $pCategoryDetail->seo_title;
        } else if (get_value($detail, 'seo_title', '')) {
            $seo_title = $detail->seo_title;
        } else {
            if (get_value($detail, 'title', '')) {
                $seo_title = $detail->title.' - '.$seo_title;
            } else if (get_value($categoryDetail, 'title', '')){
                if (get_value($pCategoryDetail, 'title', '') && get_value($pCategoryDetail, 'title', '') != get_value($categoryDetail, 'title', '')) {
                    $seo_title = $categoryDetail->title.' - '.$pCategoryDetail->title . ' - ' . $seo_title;
                } else {
                    $seo_title = $categoryDetail->title.' - '.$seo_title;
                }

            }
        }

        if (get_value($categoryDetail, 'seo_keywords', '')) {
            $seo_keywords = $categoryDetail->seo_keywords;
        } else if (get_value($pCategoryDetail, 'seo_keywords', '')) {
            $seo_keywords = $pCategoryDetail->seo_keywords;
        } else if (get_value($detail, 'seo_keywords', '')) {
            $seo_keywords = $detail->seo_keywords;
        }

        if (get_value($categoryDetail, 'seo_description', '')) {
            $seo_description = $categoryDetail->seo_description;
        } else if (get_value($pCategoryDetail, 'seo_description', '')) {
            $seo_description = $pCategoryDetail->seo_description;
        } else if (get_value($detail, 'seo_description', '')) {
            $seo_description = $detail->seo_description;
        } else {
            if (get_value($detail, 'description', '')) {
                $seo_description = $detail->description.' - '.$seo_description;
            } else if (get_value($categoryDetail, 'description', '')){
                $seo_description = $categoryDetail->description.' - '.$seo_description;
            }
        }

        $this->withData('seo', ['title'=>$seo_title, 'keywords'=>$seo_keywords, 'description'=>$seo_description]);

        return $this;
    }
}